<template>
  <div class="xys-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <xys-bread>
        <xys-bread-item to="/">首页</xys-bread-item>
        <xys-bread-item :to="`/category/${goods.categories[1].id}`">{{
            goods.categories[1].name
          }}</xys-bread-item>
        <xys-bread-item :to="`/category/sub/${goods.categories[0].id}`">{{
            goods.categories[0].name
          }}</xys-bread-item>
        <xys-bread-item>{{ goods.name }}</xys-bread-item>
      </xys-bread>
      <!-- 商品信息 -->
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品左边 -->
        <div class="goods-info-left">
          <GoodsImage :images="goods.mainPictures"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <!-- 商品右边 -->
        <div class="goods-info-right">
          <!-- 名字区组件 -->
          <good-name :goods="goods"></good-name>
          <!-- 规格组件 -->
          <!-- :skuId="1369155864430120962" 为测试用的['蓝色','中国','10cm'] -->
          <goods-sku :goods="goods" @change="getSku"></goods-sku>
          <!-- 数量选择，max 最大值为商品库存 -->
          <xtx-numbox
              :max="goods.inventory"
              v-model="count"
              label="数量"
          ></xtx-numbox>
          <!-- 加入购物车按钮 -->
          <xtx-button type="primary" style="margin-top: 20px"
                     @click="addToCartBtn "
          >加入购物车</xtx-button
          >
        </div>
      </div>
    </div>
  </div>
  <div class="loading" v-else></div>
</template>

<script setup>

import GoodsImage from "@/views/goods/components/goods-image.vue";
import GoodsSales from "@/views/goods/components/goods-sales.vue";
import GoodName from "@/views/goods/components/goods-name.vue";
import GoodsSku from "@/views/goods/components/goods-sku.vue";
import {provide, ref, watch} from "vue";
import {useRoute} from "vue-router";
import {findGoods} from "@/api/product";
import Message from "@/components/library/Message";
import {useCartStore} from "@/stores/cart";
import XtxButton from "@/components/library/xys-button.vue";
import XtxNumbox from "@/components/library/xys-numbox.vue";
import XysBread from "@/components/library/xys-bread.vue";
import XysBreadItem from "@/components/library/xys-bread-item.vue";

const goods = ref(null)
const count = ref(1)
const route = useRoute()
watch(
    () => route.params.id,
    (id) => {
      goods.value = null
      count.value = 1
      if (route.path === `/product/${id}`) {
        findGoods(id).then((res) => {
          // console.log(res)
          goods.value = res.result
        })
      }
    },
    { immediate: true }
)
const getSku = (val) => {
  console.log(val)
  if (val.skuId) {
    goods.value.inventory = val.inventory
    goods.value.oldPrice = val.oldPrice
    goods.value.price = val.price
    // 存储当前 skuId，加入购物车的时候要用到
    goods.value.currSkuId = val.skuId
    goods.value.attrsText = val.attrsText
  } else {
    // 没有选择完整规格就重置为空
    goods.value.currSkuId = ''
  }
}
const {addCart}=useCartStore()
// 加入购物车按钮
const addToCartBtn = () => {
  // 判断是否选择了完整规格
  if (!goods.value.currSkuId) return Message({ type: 'warn', message: '请选择完整规格' })
  if (count.value > goods.value.inventory) return ({ type: 'warn', message: '库存不足了' })
  // 约定本地存储的信息内容 - 字段名可参考购物车列表接口的字段名
  const cartItem = {
    id: goods.value.id,
    name: goods.value.name,
    picture: goods.value.mainPictures[0],
    price: goods.value.price,
    count: count.value,
    skuId: goods.value.currSkuId,
    attrsText: goods.value.attrsText,
    selected: true,
    nowPrice: goods.value.price,
    stock: goods.value.inventory || goods.value.stock, // 库存字段兼容性写法(库存在不同接口字段名有差别)
    isCollect: false,
    discount: 1,
    isEffective: true
  }
 addCart(cartItem)
  Message({ type: 'success', message: '添加成功' })
    // console.log('加入购物车成功后提示', res)


  console.log(goods, cartItem)
  // 编写actions进行添加操作 - 考虑到后期需要异步修改，所以再封装 actions
  // 在商品详情页实现添加逻辑
}
provide('goods', goods)
</script>

<style scoped lang='less'>
.loading {
  margin-top: 72px;
  min-height: 600px;
  background: #fff url(@/assets/images/loading.gif) no-repeat center;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
// .goods-tabs {
//   min-height: 600px;
//   background: #fff;
// }
// .goods-warn {
//   min-height: 600px;
//   background: #fff;
//   margin-top: 20px;
// }
</style>
